﻿#region Using

using System;
using System.Globalization;
using System.Text.RegularExpressions;

using GameCommon.ServiceInterfaces;

using Microsoft.Xna.Framework;

#endregion

namespace GameCommon.Services
{
    internal class ColorService : IColorService
    {
        public Color HexToColor(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return new Color();
            }

            value = value.ToUpper();
            var matches = new Regex("#(?<red>[0-9|A-F]{2})(?<green>[0-9|A-F]{2})(?<blue>[0-9|A-F]{2})").Matches(value);

            if (matches.Count == 0)
            {
                throw new FormatException(value);
            }

            var red = this.GetIntFromHex(matches[0].Groups["red"].Value);
            var green = this.GetIntFromHex(matches[0].Groups["green"].Value);
            var blue = this.GetIntFromHex(matches[0].Groups["blue"].Value);

            return new Color(red, green, blue);
        }

        private int GetIntFromHex(string asd)
        {
            return int.Parse(asd, NumberStyles.AllowHexSpecifier);
        }
    }
}